package com.example.ggpicturegg;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.ggpicturegg.Mysql_com.UserDao;
import com.example.ggpicturegg.Mysql_com.Userinfo;
import com.example.ggpicturegg.first.RecyclerviewActivity;
import com.example.ggpicturegg.test.ImagePath;
import com.example.ggpicturegg.tool.CommonUtils;

public class Login extends AppCompatActivity {

    private EditText et_uname,et_upass;    // 输入框
    private Button btn_login;   //用户登录
    private Button btn;

    private Handler mainHandler  ;  //主线程
    private UserDao dao;      // 用户数据库操作类


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }


    private  void initView(){

        et_upass = findViewById(R.id.upass);
        et_uname= findViewById(R.id.uname);
        btn_login = findViewById(R.id.btn_login);

        mainHandler = new Handler(getMainLooper()) ; //获取主线程
        dao = new UserDao();
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doLogin();
            }
        });


    }

    // 执行登录操作
    private void doLogin(){
        final String uname = et_uname.getText().toString().intern();
        final String upass = et_upass.getText().toString().intern();
        if(TextUtils.isEmpty(uname)){
            CommonUtils.showShortMsg(Login.this,"请输入用户名");
            et_uname.requestFocus();
        }else if(TextUtils.isEmpty(upass)){
            CommonUtils.showShortMsg(this,"请输入用户密码");
            et_upass.requestFocus();
        }else{
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final Userinfo item = dao.getUserByUnameAndUpass(uname,upass);

                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if(item == null ){
                                CommonUtils.showShortMsg(Login.this,"用户名或者密码错误");
                            }
                            else{
                                CommonUtils.showShortMsg(Login.this,"登录成功");
                                Intent intent = new Intent(Login.this, RecyclerviewActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putString("name",item.getUname());
                                intent.putExtras(bundle);
                                startActivity(intent);
                            }
                        }
                    });
                }
            }).start();
        }


    }
}